package com.sxfech.project.auth.service;

import com.sxfech.project.auth.entity.LoginUser;
import com.sxfech.project.auth.repository.LoginUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class OAuthUserDetailService implements UserDetailsService {
    @Autowired
    private LoginUserRepository userRepository;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LoginUser user = userRepository.findOneByUsername(username).orElseThrow(()->{
            return new UsernameNotFoundException("用户没有找到。");
        });

        return new UserContext(
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                new ArrayList<SimpleGrantedAuthority>()
        );
    }
}
